﻿@inject NavigationManager nm
@inject IJSRuntime js

<Control HasIconsRight>
    <Input @ref="kwInput" class="@B.MB2" BindEvent="oninput" placeholder="Search Components" @bind-Value="kw" @onkeypress="inputHandle"/>
    @if (kw.Length > 0)
    {
        <Icon IsRight IconClass="fa fa-times" class="@B.Clickable" @onclick="clear"></Icon>
    }
</Control>
<Menu OnItemClick="OnMenuClick">
    @foreach (var item in MenuData)
    {
        <MenuLabel>@item.Label</MenuLabel>
        <MenuList>
            @foreach (var sub in item.Children)
            {
                var flag = string.IsNullOrEmpty(kw)
                           || !string.IsNullOrEmpty(kw) 
                           && (sub.Keywords.Any(x => x.StartsWith(kw.ToLower()))
                               || item.Keywords.Any(x => x.StartsWith(kw.ToLower())));
                var cls = flag ? "" : B.Hidden;
                <MenuItem class="@cls"><MenuLink href="@sub.Href">@sub.Label
                                           @if (sub.Tag.Equals("new"))
                                           {
                                               <Tag class="px-1 ml-1" style="height: auto" Color="Color.Danger">New</Tag>     
                                           }
                                           else if (sub.Tag.Equals("upd"))
                                           {
                                               <Tag class="px-1 ml-1" style="height: auto" Color="Color.Info">Upd</Tag>
                                           }
                </MenuLink></MenuItem>
            }
        </MenuList>
    }
</Menu>

@code {

    class MItem
    {
        public string Label { get; set; }
        public string Href { get; set; }
        public HashSet<string> Keywords { get; set; }
        public string Tag { get; set; }
        public List<MItem> Children { get; set; }

        public MItem(string label, string href = "", string kws = "",string tag ="")
        {
            Label = label;
            Href = href;
            Tag = tag;
            Keywords = new HashSet<string>(kws.Split(',', StringSplitOptions.RemoveEmptyEntries));
        }
    }

    void clear()
    {
        kw = "";
    }

    void inputHandle(KeyboardEventArgs e)
    {
        if (e.Key == "Enter")
        {
            kw = kwInput.Value;
        }
    }

    string kw = "";
    Input<string> kwInput;


    private List<MItem> MenuData = new()
    {
        new MItem("概述(Overview)", "", "start,开始,概述")
        {
            Children = new List<MItem>()
            {
                new MItem("开始使用", "/docs/overview/start", ""),
                new MItem("Helpers", "/docs/overview/commons", "common,helper,color,size,颜色,尺寸"),
                new MItem("自定义主题", "/docs/overview/theme", "主题,theme,样式,自定义"),
            }
        },
        new MItem("列(Columns)", "", "column,列")
        {
            Children = new List<MItem>()
            {
                new MItem("基础", "/docs/columns/basics", "base"),
                new MItem("尺寸", "/docs/columns/sizes", "size"),
                new MItem("响应式", "/docs/columns/responsiveness", "responsiveness,response,响应"),
                new MItem("嵌套", "/docs/columns/nesting", "nest,nesting,嵌套"),
                new MItem("列间隙", "/docs/columns/gap", "gap,间隙"),
                new MItem("选项", "/docs/columns/options", "option"),
            }
        },
        new MItem("布局(Layout)", "", "layout,布局")
        {
            Children = new List<MItem>()
            {
                new MItem("容器(Container)", "/docs/layout/container", "容器,container"),
                new MItem("水平(Level)", "/docs/layout/level", "水平,level"),
                new MItem("媒体(Media)", "/docs/layout/mediaobject", "media,媒体,文章,article"),
                new MItem("雄伟(Hero)", "/docs/layout/hero", "hero,雄伟,壮观,显著"),
                new MItem("节(Section)", "/docs/layout/section", "section,节,部分"),
                new MItem("页脚(Footer)", "/docs/layout/footer", "脚,页脚,foot,footer"),
                new MItem("块(Tile)", "/docs/layout/tiles", "块,Tile"),
                new("分割线(Divider)", "/docs/layout/divider", "divider,分割,分割线")
            }
        },
        new MItem("表单(Form)", "", "表单,form")
        {
            Children = new List<MItem>()
            {
                new MItem("通用(General)", "/docs/form/general", "general,edit"),
                new MItem("输入框(Input)", "/docs/form/input", "input,text,输入,文本"),
                new MItem("计数器(NumberInput)", "/docs/form/numberinput", "number,numberinput,计数器","new"),
                new MItem("文本域(Textarea)", "/docs/form/textarea", "textarea,文本,text,域"),
                new MItem("下拉框(Select)", "/docs/form/select", "select,下拉框"),
                new MItem("级联选择(Cascader)", "/docs/form/cascader", "cascader,级联选择","new"),
                new MItem("复选框(CheckBox)", "/docs/form/checkbox", "checkbox,复选"),
                new MItem("单选框(Radio)", "/docs/form/radio", "单选,radio"),
                new MItem("开关(Switch)", "/docs/form/switch", "开关,switch"),
                new MItem("滑块(Slider)", "/docs/form/slider", "slider,滑块"),
                new MItem("文件(File)", "/docs/form/file", "file,上传,文件"),
                new MItem("标签输入框(TagsInput)", "/docs/form/tagsinput", "input,tag,tags,tagsinput,标签"),
                new MItem("日期选择(DatePicker)", "/docs/form/datepicker", "date,datepicker,picker,日期,日期选择"),
                new MItem("颜色选择(ColorPicker)", "/docs/form/colorpicker", "color,colorpicker,picker,颜色,颜色选择"),
                new MItem("穿梭框(Transfer)", "/docs/form/transfer", "transfer,穿梭框"),
                new MItem("评分(Rate)", "/docs/form/rate", "rate,评分,打分,start"),
                new MItem("Markdown编辑器(TuiEditor)", "/docs/form/tuieditor", "editor,tui,编辑器,WYSIWYG,Markdown,所见即所得"),
                new MItem("富文本编辑器(WangEditor)", "/docs/form/wangeditor", "editor,wangEditor,编辑器,WYSIWYG"),
            }
        },
        new MItem("元素(Elements)", "", "元素,element")
        {
            Children = new List<MItem>()
            {
                new MItem("块(Block)", "/docs/elements/block", "block,块"),
                new MItem("盒子(Box)", "/docs/elements/box", "box,盒子,箱子"),
                new MItem("按钮(Button)", "/docs/elements/button", "button,按钮"),
                new MItem("内容(Content)", "/docs/elements/content", "content,内容"),
                new MItem("删除(Delete)", "/docs/elements/delete", "delete,删除,remove"),
                new MItem("图标(Icon)", "/docs/elements/icon", "icon,图标,icon"),
                new MItem("图片(Image)", "/docs/elements/image", "image,图片,"),
                new MItem("通知(Notification)", "/docs/elements/notification", "notification,notify,通知"),
                new MItem("进度条(Progress)", "/docs/elements/progress", "progress,进度条"),
                new MItem("表格(Table)", "/docs/elements/table", "table,表格"),
                new MItem("标签(Tag)", "/docs/elements/tag", "tag,标签,label"),
                new MItem("徽章(Badge)", "/docs/elements/badge", "badge,徽章"),
                new MItem("工具提示(Tooltip)", "/docs/elements/tooltip", "提示,工具,tool,tip,tips,tooltip,tooltips"),
                new MItem("块状列表(BlockList)", "/docs/elements/blocklist", "ul,li,block,列表,块状"),
                new MItem("页面加载(Pageloader)", "/docs/elements/pageloader", "提示,loader,load,加载"),
                new MItem("页头(PageHeader)", "/docs/elements/pageheader", "pageheader,page,header,页头","new"),
                new MItem("标题(Title)", "/docs/elements/title", "title,标题,h1,h2,h3,h3,h4,h6"),
            }
        },
        new MItem("组件(Components)", "", "组件,com,component")
        {
            Children = new List<MItem>()
            {
                new MItem("数据表(DataTable)", "/docs/components/datatable", "table,data,datatable,表格"),
                new MItem("树形组件(Tree)", "/docs/components/tree", "tree,树形组件","new"),
                new MItem("面包屑(Breadcrumb)", "/docs/components/breadcrumb", "breadcrumb,面包屑,导航"),
                new MItem("卡片(Card)", "/docs/components/card", "card,卡片"),
                new MItem("日历(Calendar)", "/docs/components/calendar", "calendar,日历","new"),
                new MItem("下拉(Dropdown)", "/docs/components/dropdown", "ddl,dropdown,下拉"),
                new MItem("菜单(Menu)", "/docs/components/menu", "menu,菜单"),
                new MItem("消息(Message)", "/docs/components/message", "message,消息,msg"),
                new MItem("对话框(Dialog/Modal)", "/docs/components/modal", "modal,对话框,弹层,dialog,弹框,alert,confirm,prompt"),
                new MItem("导航(Navbar)", "/docs/components/navbar", "navbar,导航"),
                new MItem("分页(Pagination)", "/docs/components/pagination", "pagination,page,分页"),
                new MItem("面板(Panel)", "/docs/components/panel", "panel,面板"),
                new MItem("Tab切换(Tabs)", "/docs/components/tabs", "tabs,切换,tab"),
                new MItem("时间轴(Timeline)", "/docs/components/timeline", "time,timeline,时间,时间轴"),
                new MItem("步骤(Steps)", "/docs/components/steps", "step,steps,步骤,一步,向导","upd"),
                new MItem("轮播(Carousel)", "/docs/components/carousel", "carousel,轮播,幻灯片,跑马灯"),
                new MItem("折叠面板(Collapse)", "/docs/components/collapse", "collapsible,collapse,折叠,手风琴,accordion"),
                new MItem("快速预览(QuickView)", "/docs/components/quickview", "quickview,view,预览"),
            }
        },
    };


    private bool collapseNavMenu = true;

    private string NavMenuCssClass => collapseNavMenu ? "collapse" : null;

    private async Task OnMenuClick(MenuItem item)
    {
        @* var ele = await JSInterop.GetElementById("layoutContent"); *@
        @* await JSInterop.ScrollTo(ele, 0, 0); *@
        await js.InvokeVoidAsync("docScrollTop");
        await MenuClick.InvokeAsync();
    }

    [Parameter]
    public EventCallback MenuClick { get; set; }

}